package com.central.apps.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.app.common.constant.AppConstant;
import com.central.app.service.HandlerService;
import com.central.apps.dao.OnlineEditDao;
import com.central.apps.entity.OnlineEdit;
import com.central.apps.service.OnlineEditService;
import com.central.bizcommon.model.log.LoginLog;
import com.central.common.web.PageResult;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JHY
 */
@Service
public class OnlineEditServiceImpl extends ServiceImpl<OnlineEditDao, OnlineEdit> implements OnlineEditService {
    @Autowired
    private HandlerService handlerService;

    @Resource
    private OnlineEditDao onlineEditDao;

    @Override
    public PageResult<OnlineEdit> findPage(Map<String, Object> params) {
        Page<Map<String,Object>> page = new Page<>(1, 10);
        if (MapUtils.getInteger(params, AppConstant.BIZ_QUERY_PAGE) != null && MapUtils.getInteger(params, AppConstant.BIZ_QUERY_LIMIT) != null) {
            page = new Page<>(MapUtil.getInt(params, AppConstant.BIZ_QUERY_PAGE), MapUtil.getInt(params, AppConstant.BIZ_QUERY_LIMIT));
        }
        IPage<OnlineEdit> pageInfo = onlineEditDao.findList(page, params);
        return PageResult.<OnlineEdit>builder().data(pageInfo.getRecords()).code(0).count(pageInfo.getTotal()).build();
    }

    @Override
    public Map<String, String> getFilePath(String fileid, String appname) {
        List<Map<String, Object>> fileList = handlerService.findBySql("select * from tb_file where appname = '" + appname + "' and fileid =" + fileid + "");
        String deptfullname = MapUtil.getStr(fileList.get(0), "deptfullname");
        String filepath = MapUtil.getStr(fileList.get(0), "filepath");
        String pathid = MapUtil.getStr(fileList.get(0), "pathid");
        String extensionname = MapUtil.getStr(fileList.get(0), "extensionname");

        // 文件原名称
        String filerealname = MapUtil.getStr(fileList.get(0), "filerealname");
        // 文件md5值
        String md5 = MapUtil.getStr(fileList.get(0), "md5");
        String fileUserId = MapUtil.getStr(fileList.get(0), "userid");
        String fileUserName = MapUtil.getStr(fileList.get(0), "username");
//        List<Map<String, Object>> pathList = handlerService.findBySql("select * from tb_path where pathid =" + pathid + "");
//        String parentname = MapUtil.getStr(pathList.get(0), "parentname");
//        String pathname = MapUtil.getStr(pathList.get(0), "pathname");
        String pathname = "/home/eam/doclinks/" + deptfullname;
        String parentname = "/home/eam/doclinks";
        String downloadurl = parentname + filepath;
        String[] filenameSplit = filepath.split("/");
        String filename = "";
        if (filenameSplit.length > 0) {
            filename = filenameSplit[2];
        }

        Map<String, String> data = new HashMap<>();
        data.put("filepath", filepath);
        data.put("filerealname", filerealname);
        data.put("md5", md5);
        data.put("fileUserName", fileUserName);
        data.put("fileUserId", fileUserId);

        data.put("pathname", pathname);
        data.put("parentname", parentname);
        data.put("downloadurl", downloadurl);
        data.put("filename", filename);
        data.put("extensionname", extensionname);

        return data;
    }

    @Override
    public void updateByFileId(String md5, String id) {
        handlerService.updateBySql("UPDATE `tb_file` SET md5='" + md5 + "' where fileid = " + id);
    }
}
